﻿using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using Lib.Common;

namespace Lib.Utility
{
    public class MiscUtility
    {
        public const string MSG_DELETE_CONFIRM = "return confirm('Bạn đã chắc chắn ?');";
        public const string MSG_UPDATE_SUCCESS = "<b>Cập nhật thành công !</b>";
        public const string MSG_CONTROKEY_EXISTS = "Control key đã tồn tại";

        public static bool isValidEmail(string inputEmail)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
                return (true);
            else
                return (false);
        }
        
        public static void FillIndex(DropDownList dropIndex, int min, int max, int selected)
        {
            dropIndex.Items.Clear();
            for (int i = min; i <= max; i++)
            {
                ListItem item = new ListItem(i.ToString(), i.ToString());
                if (i == selected) item.Selected = true;
                else item.Selected = false;
                dropIndex.Items.Add(item);
            }
        }

        public static void FillIndex(DropDownList dropIndex, int max, int selected)
        {
            dropIndex.Items.Clear();
            for (int i = 0; i <= max; i++)
            {
                ListItem item = new ListItem(i.ToString(), i.ToString());
                if (i == selected) item.Selected = true;
                else item.Selected = false;
                dropIndex.Items.Add(item);
            }
        }


        public static void SelectItemFromList(ListControl list, string selectedValue)
        {
            list.SelectedIndex = -1;
            ListItem item = list.Items.FindByValue(selectedValue);
            if (item != null)
                item.Selected = true;
        }
        public static void SelectItemFromList(ListControl list, string[] selectedValues)
        {
            list.SelectedIndex = -1;
            for (int i = 0; i < selectedValues.Length; i++)
            {
                string selectedValue = selectedValues[i];
                ListItem item = list.Items.FindByValue(selectedValue);
                if (item != null)
                    item.Selected = true;
            }
        }

        public static string StringIndent(int level)
        {
            string retVal = string.Empty;
            for (int i = 0; i < level; i++)
                retVal += ".....";
            return retVal;
        }
        public static string GetSubstring(string Content, int leng)
        {
            if (Content.Length > leng)
            {
                string temp = Content.Substring(0, leng);
                temp = temp.Replace("\r", "").Replace("\n", "").Replace("\t", "");
                Content = temp.Substring(0, temp.LastIndexOf(" ")) + "...";
            }
            else
            {
                Content = Content.Replace("\r", "").Replace("\n", "").Replace("\t", "");
            }
            return Content;
        }

        public static void ByDropList(ListControl CtrlList, int j)
        {
            ArrayList arr = new ArrayList();
            for (int i = 1; i <= j; i++)
            {
                arr.Add(i);
            }
            CtrlList.DataSource = arr;
            CtrlList.DataBind();
        }



        public static void SetSelected(DropDownList dropList, string selectedValue)
        {
            dropList.SelectedIndex = -1;
            SetSelected(dropList.Items, selectedValue);
        }
        public static void SetSelected(ListItemCollection lstItems, string selectedValue)
        {
            ListItem item = lstItems.FindByValue(selectedValue);
            if (item != null) item.Selected = true;
        }
        public static string GetCPPage(string ctrlKey, string[] paramsName, string[] paramsValue)
        {
            StringBuilder sb = new StringBuilder(ObjConstants.DEFAULT_CP_PAGE);
            //sb.Append("?portalid="); sb.Append(_portalId);
            sb.Append("?ctrlkey="); sb.Append(ctrlKey);
            if (paramsName != null)
            {
                for (int i = 0; i < paramsName.Length; i++)
                {
                    sb.Append("&"); sb.Append(paramsName[i]); sb.Append("="); sb.Append(paramsValue[i]);
                }
            }
            return sb.ToString();
        }

        #region Thay thế ký tự (sử dụng trong việc rewrite URL)
        public static string Replace(string dauvao)
        {
            string daura;
            daura = dauvao.Trim();
            daura = dauvao.ToLower();
            daura = daura.Replace(" ", "-");
            daura = daura.Replace("đ", "d");
            daura = daura.Replace("â", "a");
            daura = daura.Replace("ấ", "a");
            daura = daura.Replace("ầ", "a");
            daura = daura.Replace("ẩ", "a");
            daura = daura.Replace("ẫ", "a");
            daura = daura.Replace("ậ", "a");
            daura = daura.Replace("á", "a");
            daura = daura.Replace("à", "a");
            daura = daura.Replace("ả", "a");
            daura = daura.Replace("ã", "a");
            daura = daura.Replace("ạ", "a");
            daura = daura.Replace("ă", "a");
            daura = daura.Replace("ắ", "a");
            daura = daura.Replace("ằ", "a");
            daura = daura.Replace("ẳ", "a");
            daura = daura.Replace("ẵ", "a");
            daura = daura.Replace("ặ", "a");
            daura = daura.Replace("ó", "o");
            daura = daura.Replace("ò", "o");
            daura = daura.Replace("ỏ", "o");
            daura = daura.Replace("õ", "o");
            daura = daura.Replace("ọ", "o");
            daura = daura.Replace("ô", "o");
            daura = daura.Replace("ố", "o");
            daura = daura.Replace("ồ", "o");
            daura = daura.Replace("ổ", "o");
            daura = daura.Replace("ỗ", "o");
            daura = daura.Replace("ộ", "o");
            daura = daura.Replace("ơ", "o");
            daura = daura.Replace("ờ", "o");
            daura = daura.Replace("ớ", "o");
            daura = daura.Replace("ở", "o");
            daura = daura.Replace("ỡ", "o");
            daura = daura.Replace("ợ", "o");
            daura = daura.Replace("í", "i");
            daura = daura.Replace("ì", "i");
            daura = daura.Replace("ỉ", "i");
            daura = daura.Replace("ĩ", "i");
            daura = daura.Replace("ị", "i");
            daura = daura.Replace("ý", "y");
            daura = daura.Replace("ỳ", "y");
            daura = daura.Replace("ỷ", "y");
            daura = daura.Replace("ỹ", "y");
            daura = daura.Replace("ỵ", "y");
            daura = daura.Replace("é", "e");
            daura = daura.Replace("è", "e");
            daura = daura.Replace("ẻ", "e");
            daura = daura.Replace("ẽ", "e");
            daura = daura.Replace("ẹ", "e");
            daura = daura.Replace("ê", "e");
            daura = daura.Replace("ế", "e");
            daura = daura.Replace("ề", "e");
            daura = daura.Replace("ể", "e");
            daura = daura.Replace("ễ", "e");
            daura = daura.Replace("ệ", "e");
            daura = daura.Replace("ú", "u");
            daura = daura.Replace("ù", "u");
            daura = daura.Replace("ủ", "u");
            daura = daura.Replace("ũ", "u");
            daura = daura.Replace("ụ", "u");
            daura = daura.Replace("ư", "u");
            daura = daura.Replace("ừ", "u");
            daura = daura.Replace("ứ", "u");
            daura = daura.Replace("ử", "u");
            daura = daura.Replace("ữ", "u");
            daura = daura.Replace("ự", "u");
            daura = daura.Replace(",", "-");
            daura = daura.Replace(".", "-");
            daura = daura.Replace(";", "-");
            daura = daura.Replace(":", "-");
            daura = daura.Replace("?", "-");
            daura = daura.Replace("<", "-");
            daura = daura.Replace(">", "-");
            daura = daura.Replace("`", "");
            daura = daura.Replace("~", "-");
            daura = daura.Replace("!", "-");
            daura = daura.Replace("@", "-");
            daura = daura.Replace("#", "-");
            daura = daura.Replace("$", "-");
            daura = daura.Replace("%", "-");
            daura = daura.Replace("^", "-");
            daura = daura.Replace("&", "-");
            daura = daura.Replace("*", "-");
            daura = daura.Replace("(", "-");
            daura = daura.Replace(")", "-");
            //daura = daura.Replace("-", "_");
            daura = daura.Replace("+", "-");
            daura = daura.Replace("=", "-");
            daura = daura.Replace("\\", "-");
            daura = daura.Replace("|", "-");
            daura = daura.Replace("[", "-");
            daura = daura.Replace("]", "-");
            daura = daura.Replace("{", "-");
            daura = daura.Replace("}", "-");
            daura = daura.Replace("/", "-");
            daura = daura.Replace("'", "");
            daura = daura.Replace("\"", "-");

            return daura;
        }

        #endregion
    }
}